<!--
* description: 盘点结果审核
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      v-if="initTemplate"
      ref="multipleTable"
      :dynamicButtons="tableButtons"
      :dynamicComponents="tableComponents"
      :dynamicApiConfig="apiConfig"
      :dynamicTableCols="tableCols"
      :dynamicFormFields="formField"
      :dynamicUnFormFields="unFormField"
      :dynamicIsShowSelect="true"
      :dynamicIsColumnDrop="true"
      :callbackObj="callbackObj"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import { wmsApis } from "@/api/wms/check/api";
import OneTableTemplate from "@/components/wms/templates/oneTable";
import request from "@/utils/request";
export default {
  name: "checkResultExamine",
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],
  components: {
    OneTableTemplate
  },
  watch: {
    $route(val) {
      if (
        this.formField.pageId !== "" &&
        this.$route.query.pageId !== undefined &&
        this.$route.query.pageId !== this.formField.pageId
      ) {
        this.formField.pageId = this.$route.query.pageId;
        this.formField.pageName = this.$route.query.pageName;
        this.queryTable();
      }
    }
  },
  created() {
    if (this.$route.query != null) {
      // 判断传入query参数，初始化数据
      if (this.$route.query.pageId != null && this.$route.query.pageId !== "") {
        this.formField.pageId = this.$route.query.pageId;
        this.formField.pageName = this.$route.query.pageName;
      }
    }
  },
  mounted() {
    this.querySelect();
  },
  data() {
    return {
      // 网格查询API配置对象
      apiConfig: wmsApis.queryInventory,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.queryTable(),
          name: "search",
          position: "right",
          text: "查询"
        },
        {
          compKey: "btnKey2",
          type: "",
          size: "small",
          name: "reset",
          position: "right",
          clickEvent: () => this.reset(),
          text: "重置"
        },
        {
          compKey: "btnKey3",
          type: "",
          size: "small",
          clickEvent: () => this.export(),
          name: "export",
          position: "left",
          text: "导出" //"导出"
        },
        {
          compKey: "btnKey4",
          type: "",
          size: "small",
          clickEvent: () => this.examine(),
          name: "examine",
          position: "left",
          text: "审核"
        },
        {
          compKey: "btnKey5",
          type: "",
          size: "small",
          clickEvent: () => this.closingInventory(),
          name: "closingInventory",
          position: "left",
          text: "结束盘点"
        }
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          compKey: "compKey1",
          labelName: "仓库名称",
          codeField: "WAREHOUSE_IDS",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: false,
          options: [],
          changed: val => {
            this.tableComponents[1].options = [];
            if (!val) {
              return;
            }
            request({
              url: "/mp/wm/vl/basedata/boxcommon/queryarea.do",
              method: "post",
              data: {
                AREA_TYPE: "00",
                WAREHOUSE_IDS: val
              }
            }).then(res => {
              if (res.listRow && res.listRow.length > 0) {
                res.listRow.map(item => {
                  this.tableComponents[1].options.push({
                    label: item.AREA_NAME + "-" + item.AREA_ATTR_NAME,
                    value: item.AREA_CODE
                  });
                });
              }
            });
          }
        },
        {
          compKey: "compKey2",
          labelName: "库区",
          codeField: "AREA_CODE",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: false,
          options: []
        },
        {
          compKey: "compKey3",
          labelName: "库位",
          codeField: "PLACE_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey4",
          labelName: "备件编号",
          codeField: "PART_NO",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey5",
          labelName: "盘点差异",
          codeField: "CHECK_DIFF",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: false,
          options: [
            {
              label: "盘盈",
              value: "盘盈"
            },
            {
              label: "盘亏",
              value: "盘亏"
            },
            {
              label: "盘平",
              value: "盘平"
            }
          ]
        }
      ],
      // 动态生成网格列
      tableCols: [
        {
          prop: "WAREHOUSE_NAME",
          label: "仓库名称",
          width: "100px",
          align: "center"
        },
        {
          prop: "CHECK_CODE",
          label: "盘点单号",
          width: "140px",
          align: "center"
        },
        {
          prop: "AREA_CODE",
          label: "库区编码",
          width: "100px",
          align: "center"
        },
        {
          prop: "AREA_NAME",
          label: "库区名称",
          width: "140px",
          align: "center"
        },
        {
          prop: "PLACE_CODE",
          label: "库位",
          width: "140px",
          align: "center"
        },
        {
          prop: "PART_NO",
          label: "备件编号",
          width: "140px",
          align: "center"
        },
        {
          prop: "PART_NAME",
          label: "备件名称",
          width: "140px",
          align: "center"
        },
        {
          prop: "STATUS_NAME",
          label: "盘点状态",
          width: "100px",
          align: "center"
        },
        {
          prop: "SYS_QTY",
          label: "系统数量",
          width: "140px",
          align: "center"
        },
        {
          prop: "CHECK_QTY",
          label: "盘点数量",
          width: "140px",
          align: "center"
        },
        {
          prop: "DIFF_QTY",
          label: "盘点差异",
          width: "140px",
          align: "center"
        },
        {
          prop: "CHECK_PERSON_NAME",
          label: "盘点人",
          width: "140px",
          align: "center"
        },
        {
          prop: "CHECK_TIME_NEW",
          label: "盘点时间",
          width: "140px",
          align: "center"
        },
        {
          prop: "DIFF_QTY_STR",
          label: "盘点差异状态",
          width: "140px",
          align: "center"
        }
      ],
      //表单查询数据
      formField: {
        WAREHOUSE_IDS: "",
        AREA_CODE: "",
        PLACE_CODE: "",
        PART_NO: "",
        CHECK_DIFF: "",
        STATUS: "02"
      },
      unFormField: {},
      // 回调函数
      callbackObj: {
        //  当选择项发生变化时会触发该事件
        onCheckSelectChange: selection => {
          this.checkTableList = selection;
        },
        onAfterReset: () => {
          this.tableComponents[0].changed(this.formField.WAREHOUSE_IDS);
        }
      },
      // 选中的表格数据
      checkTableList: [],
      // 控制初始化页面
      initTemplate: false
    };
  },
  methods: {
    closingInventory() {
      if (this.checkTableList.length > 0) {
        this.$confirm("确定要结束盘点吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消"
        })
          .then(() => {
            request({
              url: "/mp/wm/vl/warehouse/inventory/InventoryEnd.do",
              method: "post",
              data: {
                InventoryEnd: this.checkTableList
              }
            }).then(res => {
              if (res.result === "1") {
                this.queryTable();
                this.$message({
                  message: "结束盘点成功",
                  type: "success"
                });
              } else {
                this.$message({
                  message: res.debugMsg ? res.debugMsg : res.msg,
                  type: "warn"
                });
              }
            });
          })
          .catch(err => {
            console.info(err);
          });
      } else {
        this.$message({
          message: "请选择一条数据"
        });
      }
    },
    examine() {
      if (this.checkTableList.length > 0) {
        this.$confirm("确定要审核吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消"
        })
          .then(() => {
            request({
              url: "/mp/wm/vl/warehouse/inventory/InventoryResultsExamine.do",
              method: "post",
              data: {
                InventoryResultsExamine: this.checkTableList
              }
            }).then(res => {
              if (res.result === "1") {
                this.queryTable();
                this.$message({
                  message: "审核成功",
                  type: "success"
                });
              } else {
                this.$message({
                  message: res.debugMsg ? res.debugMsg : res.msg,
                  type: "warn"
                });
              }
            });
          })
          .catch(err => {
            console.info(err);
          });
      } else {
        this.$message({
          message: "请选择一条数据"
        });
      }
    },
    export() {
      const exportLoading = this.$loading({
        lock: true,
        text: "正在导出...",
        // spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
        fullscreen: true
      });
      request({
        url: "/mp/wm/vl/warehouse/inventory/exportInventoryExamine.do",
        method: "post",
        headers: {
          "Cache-Control": "no-cache, no-store,max-age=0, must-revalidate",
          Pragma: "no-cache",
          Expires: "0",
          "Content-Type": "application/json;charset=UTF-8"
        },
        responseType: "blob",
        data: this.formField
      })
        .then(response => {
          exportLoading.close();
          let blob = new Blob([response], { type: "application/vnd.ms-excel" }); // 这里表示xmlx类型
          let downloadElement = document.createElement("a");
          let href = window.URL.createObjectURL(blob); // 创建下载的链接
          downloadElement.href = href;
          downloadElement.download = "盘点结果审核导出.xls"; // 下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); // 点击下载
          document.body.removeChild(downloadElement); // 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放掉blob对象
        })
        .catch(err => {
          exportLoading.close();
          console.log(err);
        });
    },
    querySelect() {
      request({
        url: "/mp/wm/vl/basedata/warehouseuser/queryUserByWHBox.do",
        method: "post",
        data: {
          IS_ENABLE: "1"
        }
      }).then(res => {
        if (res.listRow && res.listRow.length > 0) {
          res.listRow.map(item => {
            this.tableComponents[0].options.push({
              label: item.WAREHOUSE_NAME,
              value: item.WAREHOUSE_ID
            });
          });
          this.formField.WAREHOUSE_IDS = res.listRow[0].WAREHOUSE_ID;
          this.tableComponents[0].changed(res.listRow[0].WAREHOUSE_ID);
        }
        this.initTemplate = true;
        this.$nextTick(() => {
          this.queryTable();
        });
      });
    }
  }
};
</script>
